package com.binvshe.binvshe.mine.view;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.binvshe.binvshe.R;
import com.binvshe.binvshe.mine.utils.AreaDataUtil;

import java.util.ArrayList;

/**
 * Created by zofnk.
 * E-mail：zofnk@gmail.com
 * 2016/9/6
 */
public class ChooseCityDialog extends DialogFragment implements View.OnClickListener {

    private ProvinceClickable mOnClickable;
    private WheelView mWheelViewProvince;
    private int mSex = 2;
    private ArrayList<String> mProvinceList;
    private WheelView mWheelViewCity;
    private AreaDataUtil mAreaDataUtil;

    private int mCurrProvinceIndex = -1;
    private int mCurrCityIndex = -1;

    @Override
    public void onStart() {
        super.onStart();
        getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        Window window = getDialog().getWindow();
        window.setWindowAnimations(R.style.AnimBottom); //设置窗口弹出动画
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag_alert_chose_city, container);
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getAreaInfo();

        TextView btnCannel = (TextView) view.findViewById(R.id.btn_cancel);
        btnCannel.setOnClickListener(this);
        View vwBlank = view.findViewById(R.id.mVwBlank);
        vwBlank.setOnClickListener(this);
        TextView btnEnter = (TextView) view.findViewById(R.id.btn_enter);
        btnEnter.setOnClickListener(this);

        mWheelViewProvince = (WheelView) view.findViewById(R.id.wvProvince);
        mWheelViewCity = (WheelView) view.findViewById(R.id.wvCity);

        //加载省份List
        mWheelViewProvince.setData(mProvinceList);
        mWheelViewProvince.setDefault(0);

        //加载城市
        String defaultProvince = mProvinceList.get(0);
        mWheelViewCity.setData(mAreaDataUtil.getCityByProvince(defaultProvince));
        mWheelViewCity.setDefault(1);

        mWheelViewProvince.setOnSelectListener(new WheelView.OnSelectListener() {
            @Override
            public void endSelect(int id, String text) {
                if (text.equals("") || text == null)
                    return;
                if (mCurrProvinceIndex != id) {
                    mCurrProvinceIndex = id;
                    String selectProvince = mWheelViewProvince.getSelectedText();
                    if (selectProvince == null || selectProvince.equals(""))
                        return;

                    ArrayList<String> city = mAreaDataUtil.getCityByProvince(mProvinceList.get(id));
                    if (city.size() == 0) {
                        return;
                    }

                    mWheelViewCity.setData(city);

                    if (city.size() > 1) {
                        //if city is more than one,show start index == 1
                        mWheelViewCity.setDefault(1);
                    } else {
                        mWheelViewCity.setDefault(0);
                    }
                }
            }

            @Override
            public void selecting(int id, String text) {

            }
        });

        mWheelViewCity.setOnSelectListener(new WheelView.OnSelectListener() {
            @Override
            public void endSelect(int id, String text) {
                if (text.equals("") || text == null)
                    return;
                if (mCurrCityIndex != id) {
                    mCurrCityIndex = id;
                    String selectCity = mWheelViewCity.getSelectedText();
                    if (selectCity == null || selectCity.equals(""))
                        return;
                    int lastIndex = Integer.valueOf(mWheelViewCity.getListSize());
                    if (id > lastIndex) {
                        mWheelViewCity.setDefault(lastIndex - 1);
                    }
                }
            }

            @Override
            public void selecting(int id, String text) {

            }
        });

        return view;
    }

    public String getProvince() {
        if (mWheelViewProvince == null) {
            return null;
        }
        return mWheelViewProvince.getSelectedText();
    }

    public String getCity() {
        if (mWheelViewCity == null) {
            return null;
        }
        return mWheelViewCity.getSelectedText();
    }

    private void getAreaInfo() {
        mAreaDataUtil = new AreaDataUtil(getActivity());
        mProvinceList = mAreaDataUtil.getProvinces();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_cancel:
                dismiss();
                break;
            case R.id.mVwBlank:
                dismiss();
                break;
            case R.id.btn_enter:
                mOnClickable.btnEnter(getProvince(), getCity());
                dismiss();
                break;
        }
    }

    public void setOnClickable(ProvinceClickable mOnClickable) {
        this.mOnClickable = mOnClickable;
    }

    public interface ProvinceClickable {
        void btnEnter(String province, String city);
    }
}
